package top.drinyeit.weilong.presenter;

import android.content.Intent;

import top.drinyeit.weilong.activity.LoginActivity;
import top.drinyeit.weilong.activity.RegistrationActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;
import top.drinyeit.weilong.util.Validator;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by bigHead on 2018/1/2.
 */

public class RegistrationPresenter {
    private RegistrationActivity registrationView;

    public RegistrationPresenter(RegistrationActivity registrationView) {
        this.registrationView = registrationView;
    }

    //获取短信验证码
    public void getCode(String login_phone) {
        if (!Validator.isMobile(login_phone)) {
            registrationView.showToastMsg("请输入正确的手机号码");
            return;
        }
        registrationView.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                registrationView.dismissLoading();
                registrationView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                registrationView.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    registrationView.setTime();
                    registrationView.showToastMsg("已成功发送验证码");
                } else {
                    registrationView.showToastMsg(o.getMessage());
                }
            }
        });

        RetrofitMethod.getInstance().get_code(subscriber, login_phone);
    }

    //注册
    public void registration(String login_phone, String code) {
        if (login_phone.length() != 11) {
            registrationView.showToastMsg("请输入正确的手机号码");
            return;
        }
        if (code.length() != 4) {
            registrationView.showToastMsg("请输入正确的验证码");
            return;
        }
        registrationView.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                registrationView.dismissLoading();
                registrationView.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                registrationView.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    SPUtil.setShareString(Constant.USER_ID, o.getUser_id());
                    registrationView.showToastMsg("注册成功");
                    Intent intent = registrationView.getIntent();
                    registrationView.setResult(1, intent);
                    registrationView.activityManager.finishActivity(LoginActivity.class);
                    registrationView.finish();
                } else {
                    registrationView.showToastMsg(o.getMessage());
                }
            }
        });

        RetrofitMethod.getInstance().login(subscriber, login_phone, code);
    }
}
